package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map count(Long userId) {
        //查找我喜欢的--喜欢
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        Long loveCount = mongoTemplate.count(query, UserLike.class);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        //查找互相喜欢
        Criteria criteria1 = Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId).and("isLike").is(true);
        Query query1 = Query.query(criteria1);
        Long eachLoveCount = mongoTemplate.count(query1, UserLike.class);
        //喜欢我的
        Criteria criteria2 = Criteria.where("likeUserId").is(userId).and("isLike").is(true);
        Query query2 = Query.query(criteria2);
        long fanCount = mongoTemplate.count(query2, UserLike.class);
        Map map = new HashMap();
        map.put("eachLoveCount", eachLoveCount);
        map.put("loveCount", loveCount);
        map.put("fanCount", fanCount);
        return map;
    }

    @Override
    public void fansLike(Long uid, Long userId) {
        UserLike userLike = new UserLike();
        userLike.setUserId(userId);
        userLike.setLikeUserId(uid);
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUpdated(System.currentTimeMillis());
        userLike.setIsLike(true);
        mongoTemplate.save(userLike);
    }

    @Override
    public void deleteLike(Long uid, Long userId) {

        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(uid).and("isLike").is(true);
        Query query = Query.query(criteria);
        UserLike one = mongoTemplate.findOne(query, UserLike.class);
        if (one != null) {
            mongoTemplate.remove(one);
        }
    }

    //分页查询互相喜欢
    @Override
    public PageResult queryEachLikeList(Integer page, Integer pagesize, Long userId) {

        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        Criteria criteria1 = Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId).and("isLike").is(true);
        Query query1 = Query.query(criteria1).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("updated")));
        long count = mongoTemplate.count(query1, UserLike.class);
        List<UserLike> likeList = mongoTemplate.find(query1, UserLike.class);
        if (CollUtil.isEmpty(likeList)) {
            return new PageResult();
        } else {
            PageResult pageResult = new PageResult();
            pageResult.setPage(page);
            pageResult.setPagesize(pagesize);
            pageResult.setCounts(count);
            pageResult.setItems(likeList);
            pageResult.setPages(count / pagesize);

            return pageResult;
        }
    }

    //分页查询我喜欢
    @Override
    public PageResult queryLikeList(Integer page, Integer pagesize, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("updated")));
        long count = mongoTemplate.count(query, UserLike.class);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        if (CollUtil.isEmpty(userLikes)) {
            return new PageResult();
        } else {
            PageResult pageResult = new PageResult();
            pageResult.setPage(page);
            pageResult.setPagesize(pagesize);
            pageResult.setCounts(count);
            pageResult.setItems(userLikes);
            pageResult.setPages(count / pagesize);
            return pageResult;
        }
    }

    @Override
    public PageResult queryfansList(Integer page, Integer pagesize, Long userId) {
        //喜欢我的
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("updated")));
        long count = mongoTemplate.count(query, UserLike.class);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        if (CollUtil.isEmpty(userLikes)) {
            return new PageResult();
        } else {
            PageResult pageResult = new PageResult();
            pageResult.setPage(page);
            pageResult.setPagesize(pagesize);
            pageResult.setCounts(count);
            pageResult.setItems(userLikes);
            pageResult.setPages(count / pagesize);
            return pageResult;
        }
    }

    @Override
    public UserLike isSave(Long userId, Long uid, boolean b) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(uid)
                .and("isLike").is(b);
        return mongoTemplate.findOne(Query.query(criteria), UserLike.class);
    }

    @Override
    public void save(UserLike user) {
        mongoTemplate.save(user);
    }

    @Override
    public void del(Long userId, Long uid) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(uid);
        mongoTemplate.remove(Query.query(criteria), UserLike.class);
    }

    @Override
    public UserLike query(Long likeUserId, Long userId) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("userId").is(likeUserId);
        UserLike one = mongoTemplate.findOne(Query.query(criteria), UserLike.class);
        return one;
    }


}

